﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Hutech.CRM.Library.Models.Entities;
using Hutech.CRM.Library.Models.DataAccessLayer;
using Hutech.CRM.Library.Models.BusinessAccessLayer.BusinessObjects;
using Hutech.CRM.Library.Utilities;
using nvn.Library.Patterns;

namespace Hutech.CRM.Library.Models.BusinessAccessLayer
{
    class AccountCustomerBAL : IAccountCustomerBAL
    {
        #region Variable

        #endregion

        #region Private Method
        /// <summary>
        /// Persists a new instance of CriterionForGroupCustomer. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create(AccountCustomer item)
        {
            item.AccountCustomerGuid = Guid.NewGuid();

            int rowsAffected = AccountCustomerDAL.Create(item);

            return (rowsAffected > 0);

        }

        /// <summary>
        /// Updates this instance of CriterionForGroupCustomer. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update(AccountCustomer item)
        {
            return AccountCustomerDAL.Update(item);
        }

        private List<AccountCustomer> LoadListFromReader(IDataReader reader)
        {
            List<AccountCustomer> accountCustomerList = new List<AccountCustomer>();

            //GETDATA ACCOUNT
            try
            {
                while (reader.Read())
                {
                    AccountCustomer accountCustomer = new AccountCustomer();
                    accountCustomer.AccountCustomerGuid = new Guid(reader["AccountCustomerGuid"].ToString());
                    accountCustomer.HoTen = reader["HoTen"].ToString();
                    accountCustomer.DiaChi = reader["DiaChi"].ToString();
                    accountCustomer.Email = reader["Email"].ToString();
                    accountCustomer.DienThoai = reader["DienThoai"].ToString();
                    accountCustomer.Level = new Guid(reader["Level"].ToString());
                    accountCustomer.Fax = reader["Fax"].ToString();
                    accountCustomer.TaiKhoanNganHang = reader["TaiKhoanNganHang"].ToString();
                    accountCustomer.MaSoThue = reader["MaSoThue"].ToString();
                    accountCustomer.IsDelete = Convert.ToBoolean(reader["IsDelete"]);
                    accountCustomer.UserUpdated = new Guid(reader["UserUpdated"].ToString());
                    accountCustomer.LastUpdatedDate = Convert.ToDateTime(reader["LastUpdatedDate"]);

                    accountCustomer.TenCongTy = reader["TenCongTy"].ToString();
                    accountCustomer.TenNganhNghe = reader["TenNganhNghe"].ToString();
                    accountCustomer.TenChucVu = reader["TenChucVu"].ToString();
                    accountCustomer.TenTinhThanhPho = reader["TenTinhThanhPho"].ToString();
                    accountCustomer.Yahoo = reader["Yahoo"].ToString();
                    accountCustomer.Skype = reader["Skype"].ToString();

                    try
                    {
                        accountCustomer.NgaySinh = Convert.ToDateTime(reader["NgaySinh"]);
                    }
                    catch (Exception) { }

                    accountCustomerList.Add(accountCustomer);
                }
            }
            finally
            {
                reader.Close();
            }



            return accountCustomerList;

        }

        #endregion

        #region Publish method
        public bool Save(AccountCustomer item)
        {
            if (item.AccountCustomerGuid != Guid.Empty)
            {
                return Update(item);
            }
            else
            {
                return Create(item);
            }
        }
        public bool Delete(Guid accountCustomerGuid)
        {
            throw new NotImplementedException();
        }
        public bool CheckExist(Guid itemGuid)
        {
            throw new NotImplementedException();
        }
        public List<AccountCustomer> GetAll()
        {
            IDataReader reader = AccountCustomerDAL.GetAll();
            return LoadListFromReader(reader);
        }

        public List<AccountCustomer> GetPage(int PageNumber, int PageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = AccountCustomerDAL.GetPage(PageNumber, PageSize, out totalPages);
            return LoadListFromReader(reader);
        }
        public List<AccountCustomer> GetPageForSearch(
            string kindSearch,
            string keySearch,
            Guid Job,
            Guid Position,
            Guid Province,
            Guid Level,
            int PageNumber, int PageSize, out int totalPages)
        {
            totalPages = 1;

            IDataReader reader = AccountCustomerDAL.GetPageForSearch(
                kindSearch,
                keySearch,
                Job,
                Position,
                Province,
                Level,
                PageNumber, PageSize, out totalPages);

            return LoadListFromReader(reader);
        }
        #endregion


        
    }
}
